<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this -> middleware('login');
    }
    
    public function index(Request  $request)
    {
        //
        $num = $request -> input('num', 2);
        $keywords = $request -> input('keywords', '');

        $data = DB::table('shop_type as c1') 
        -> select('c1.*', 'c2.name as pname', DB::raw("concat(c1.path, ',', c1.id) as sortpath"))
        -> leftJoin('shop_type as c2', 'c1.pid', '=', 'c2.id')
        -> where('c1.name', 'like', '%'.$keywords.'%')
        -> orderBy('sortpath') 
        -> paginate($num);
   
        foreach($data as $key => $value){
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|---', $num);
            $data[$key] -> name = $str.$value -> name;
        }

        return view('admin.category.index', ['data' => $data, 'request' => $request -> all(), 'title' => '分类列表']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data = DB::table('shop_type as c1') 
        -> select('c1.*', 'c2.name as pname', DB::raw("concat(c1.path, ',', c1.id) as sortpath"))
        -> leftJoin('shop_type as c2', 'c1.pid', '=', 'c2.id')
        -> orderBy('sortpath')
        -> get();

        foreach($data as $key => $value){
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|---', $num);
            $data[$key] -> name = $str.$value -> name;
        }


        return view('admin.category.add', ['title' => '分类添加'], ['data' => $data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request -> except('_token');

        $parent_path = DB::table('shop_type') -> where('id', $data['pid']) -> first() -> path;
        $data['path'] = $parent_path.','.$data['pid'];

        $time = date('Y-m-d H:i:s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;

        $res = DB::table('shop_type') -> insert($data);

        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '添加成功']);
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = DB::table('shop_type as c1') 
        -> select('c1.*', 'c2.name as pname', DB::raw("concat(c1.path, ',', c1.id) as sortpath"))
        -> leftJoin('shop_type as c2', 'c1.pid', '=', 'c2.id')
        -> orderBy('sortpath')
        -> get();

        $cateData = DB::table('shop_type') -> where('id', $id) -> first();


        foreach($data as $key => $value){
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|---', $num);
            $data[$key] -> name = $str.$value -> name;
        }

        return view('admin.category.edit', ['title' => '编辑分类'], ['data' => $data, 'cateData' => $cateData]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //$data = $request -> except('_token', '_method', 'pid');
        $data = $request -> except('_token', '_method');
        if($data['pid'] != $id)
        {
            $parent_path = DB::table('shop_type') -> where('id', $data['pid']) -> first() -> path;

            $data['path'] = $parent_path.','.$data['pid'];
        }else
        {
            $data = $request -> except('_token', '_method', 'pid');
        }
        
        $time = date('Y-m-d H:i:s');
        $data['updated_at'] = $time;

        $res = DB::table('shop_type') ->where('id', $id) -> update($data);

        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '编辑成功']);
        }else
        {
            return back() -> with(['info' => '编辑失败']);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = DB::table('shop_type') -> where('id', $id) -> delete();

        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
    }
}
